namespace ShellNavigator.Client.Utils
{
  #region

  using System;
  using System.Windows.Input;

  #endregion

  public class DelegateCommand : ICommand
  {
    #region Constants and Fields

    private readonly Action<object> action;

    private readonly Func<object, bool> preview;

    #endregion

    #region Constructors and Destructors

    public DelegateCommand(Action<object> action, Func<object, bool> preview)
    {
      this.action = action;
      this.preview = preview;
    }

    #endregion

    #region Public Events

    public event EventHandler CanExecuteChanged;

    #endregion

    #region Public Methods

    public bool CanExecute(object parameter)
    {
      return this.preview == null || this.preview(parameter);
    }

    public void Execute(object parameter)
    {
      if (this.action != null)
      {
        this.action(parameter);
      }
    }

    /// <summary>
    /// Invalidates the preview.
    /// </summary>
    public void InvalidatePreview()
    {
      if (this.CanExecuteChanged != null)
      {
        this.CanExecuteChanged(this, new EventArgs());
      }
    }

    #endregion
  }
}